import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import uuidv1 from 'uuid/v1'
import ViewUI from 'view-design';
import publicFun from './plugins/publiction.js'
import config from '@/config/index.js'
import Message from '@/plugins/message.js'
import 'view-design/dist/styles/iview.css';

import CheckBox from '@/components/CheckBox.vue';
import Loading from '@/components/Loading.vue'
import CheckIcon from '@/components/CheckIcon.vue'

import '@/assets/css/reset.css';
import '@/assets/css/publication.css';
import '@/assets/css/icon/iconfont.css';

const NODE = process.env.VUE_APP_NODE;

Vue.use(ViewUI);

const loginApi = axios.create({
  baseURL: config.oauthUrl,
  headers: {
    Authorization: config.oauthAuthorization,
  }
})
const mainApi = axios.create({
  baseURL: config.apiUrl,
})


mainApi.interceptors.response.use(function (response) {  //响应拦截器
  if (response.data.code === -2 || response.data.code === -3) {
    Message({
      type: 'error',
      message: '登陆过期，请重新登录！'
    })
    // window.location.href = config.loginUrl
    if (NODE === 'production') {
      return window.location.href = config.loginUrl + '?redirect_uri=' + encodeURIComponent(`${window.location.href}`)
    } else {
      return router.push({path: '/setting/login'});
    }
  }else if(response.data.code !== 1){
    Message({
      type: 'error',
      message: response.data.msg
    })
  }
  return response
}, function (error) {  //响应错误处理console.log('error');
  Message({
    type: 'error',
    message: '网络错误'
  })
  return error
})
Vue.config.productionTip = false
Vue.prototype.$config = config
Vue.prototype.ceshi = '1fgfg51sad'
Vue.http = Vue.prototype.$http = mainApi
Vue.prototype.$loginApi = loginApi
Vue.prototype.$uuidv1 = uuidv1
Vue.prototype.$message = Message

Vue.directive('focus', {  //自动获取焦点并全选
  inserted: function (el) {
    el.focus()
    el.select()
  }
})

Vue.use(publicFun)
Vue.component('check-box',CheckBox)
Vue.component('loading',Loading)
Vue.component('check-icon',CheckIcon)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
